
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<div class="modal" id="coaDiv" tabindex="-1" role="dialog" aria-hidden="true">

	<div class="modal-dialog" role="document">

		<div class="modal-content animated bounceInRight">

			<div class="modal-header">
				<h4 class="modal-title">会计科目结构</h4>
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>

			<div class="modal-body" style="padding-bottom: 20px;">
				
				<div class="col-sm-12">
					<%-- 导入提示信息框 --%>
				    <c:if test="${hint!=null&&hint!=''}">
				   		<jsp:include page="../../common/alert/alert.jsp">
				   			<jsp:param value="${hint}" name="alertType"/>
				   			<jsp:param value="${alertMessage}" name="alertMessage"/>
				   		</jsp:include>
				    </c:if>
				    
				    <form id="lineForm">
				    	<c:forEach items="${requestScope.coa}" var="coa">
				    		<c:if test="${coa.segmentDataType=='URL'}">
				    			<div class="form-group row">
									<label class="col-sm-3 col-form-label">${coa.segmentFieldName}</label>
									<div class="col-sm-9">
										<input id="${coa.segmentField}" type="text" class="form-control ${coa.segmentFieldType} segment" value="${coa.segmentFieldValue}" />
										<span id="${coa.segmentField}Desc" class="help-block m-b-none" style="color: silver;">${coa.segmentFieldValueDesc}</span>
									</div>
								</div>
				    		</c:if>
							<c:if test="${coa.segmentDataType=='MAP'}">
				    			<div class="form-group row">
									<label class="col-sm-3 col-form-label">${coa.segmentFieldName}</label>
									<div class="col-sm-9">
										<select class="chosen-select form-control segment" id="${coa.segmentField}">
				                        	<option value="0" selected="selected">无（0）</option>
				                        	<c:forEach items="${coa.segmentDataMap}" var="segmentData">
				                        		<option value="${segmentData.key}">${segmentData.value} (${segmentData.key})</option>
				                        	</c:forEach>
				                        </select>
				                        <script>
				                        if("${coa.segmentFieldValue}"!=""){
				                        	$("#${coa.segmentField}").val("${coa.segmentFieldValue}");
				                        }
				                        </script>
									</div>
								</div>
				    		</c:if>
						</c:forEach>
						
						<div class="hr-line-dashed"></div>
					</form>
				</div>
				
				<div class="form-group row m-b-none">
					<div class="col-sm-12 col-sm-offset-2 text-right">
						<button class="btn btn-white btn-lg" type="button" data-dismiss="modal">返回</button>
						&nbsp;
						<button id="selectButton" class="ladda-button ladda-button-demo btn btn-primary btn-lg" data-style="expand-right">
							&nbsp;&nbsp;确定&nbsp;&nbsp;<i class="fa fa-check-square-o"></i>
						</button>
					</div>
				</div>

			</div>

		</div>

	</div>

</div>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<div id="coaTreeModal"></div>

<script>
	$(document).ready(function() {
	
		//初始化chosen-select
		$('.chosen-select').chosen({width: "100%"});
		
		//为模态对话框添加拖拽
		$("#coaDiv").find(".modal-dialog").draggable();
        $("#coaDiv").css("overflow", "hidden");
		
		//科目选择框
		$(".SEGMENT_SUBJECT").click(function(){
			var id = $(this).attr("id");
			$.ajax({
				type: "post",
				url: "web/gLCodeCombinations/subjectTreeModal",
				data: {"id": id},
				async: false,
				dataType: "html",
				cache: false,
				success: function(data){
					if(data!=""){
						$("#coaTreeModal").html(data);
						$('#subjectTreeDiv').modal('show');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					redragonJS.alert(errorThrown);
				}
			});
		});
		
		//明细科目选择框
		$(".SEGMENT_DETAIL_SUBJECT").click(function(){
			var id = $(this).attr("id");
			$.ajax({
				type: "post",
				url: "web/gLCodeCombinations/subjectDetailTreeModal",
				data: {"id": id},
				async: false,
				dataType: "html",
				cache: false,
				success: function(data){
					if(data!=""){
						$("#coaTreeModal").html(data);
						$('#subjectDetailTreeDiv').modal('show');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					redragonJS.alert(errorThrown);
				}
			});
		});
		
		//公司选择框
		$(".SEGMENT_COMPANY").click(function(){
			var id = $(this).attr("id");
			$.ajax({
				type: "post",
				url: "web/gLCodeCombinations/companyTreeModal",
				data: {"id": id},
				async: false,
				dataType: "html",
				cache: false,
				success: function(data){
					if(data!=""){
						$("#coaTreeModal").html(data);
						$('#companyTreeDiv').modal('show');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					redragonJS.alert(errorThrown);
				}
			});
		});
		
		//成本中心选择框
		$(".SEGMENT_COST_CENTER").click(function(){
			var id = $(this).attr("id");
			$.ajax({
				type: "post",
				url: "web/gLCodeCombinations/costCenterTreeModal",
				data: {"id": id},
				async: false,
				dataType: "html",
				cache: false,
				success: function(data){
					if(data!=""){
						$("#coaTreeModal").html(data);
						$('#costCenterTreeDiv').modal('show');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					redragonJS.alert(errorThrown);
				}
			});
		});
		
		//往来选择框
		$(".SEGMENT_INTER_COMPANY").click(function(){
			var id = $(this).attr("id");
			$.ajax({
				type: "post",
				url: "web/gLCodeCombinations/companyTreeModal",
				data: {"id": id},
				async: false,
				dataType: "html",
				cache: false,
				success: function(data){
					if(data!=""){
						$("#coaTreeModal").html(data);
						$('#companyTreeDiv').modal('show');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					redragonJS.alert(errorThrown);
				}
			});
		});
	
		//确认按钮
		$("#selectButton").click(function(){
			var segmentValue = "";
			var submitFlag = "Y";
			
			$(".segment").each(function(){
				if($(this).val()==""){
					submitFlag = "N";
					return false;
				}else{
					if(segmentValue==""){
						segmentValue = "{\""+$(this).attr("id")+"\":\""+$(this).val()+"\"";
					}else{
						segmentValue = segmentValue+",\""+$(this).attr("id")+"\":\""+$(this).val()+"\"";
					}
					
				}
			});
			
			segmentValue = segmentValue+"}";
			
			//提交
			if(submitFlag=="N"){
				redragonJS.alert("会计科目结构所有段值必填");
			}else{	
				$('#coaDiv').modal('hide');
				$.ajax({
					type: "post",
					url: "web/gLCodeCombinations/getOrInsertGLCodeCombinations",
					data: $.parseJSON(segmentValue),
					async: false,
					dataType: "json",
					cache: false,
					success: function(data){
						if(data!=""){
							$(selectSubjectInput).val(data.segment);
							$(selectSubjectInput).attr("title", data.segmentDesc);
							$(selectSubjectInput).parent().find("input[name='subjectCode']").val(data.codeCombinationCode);
							$(selectSubjectInput).parent().find(".subjectDescTitle").text(data.segmentDesc);
							
							$(selectSubjectInput).parent().find("input[name='subjectCode']").trigger("change");
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						redragonJS.alert(textStatus);
					}
				});
			}
		});
	});
</script>
